package com.zjitc.servicesms.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.zjitc.servicesms.service.SmsService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * @Author: ${赵诚挚}
 * @Date: 2024/10/9 14:23
 */
@Service
public class SmsServiceImpl implements SmsService {
    @Override
    public boolean getPhone(Map<String, Object> param, String phone) {
            if(StringUtils.isEmpty(phone)) return false;

            DefaultProfile profile =
                    DefaultProfile.getProfile("default", "LTAI5tHmDrT9umgsH8WhckAa", "cB9HatDy1CKsWgVAdx33AysZPqSyIo");
            IAcsClient client = new DefaultAcsClient(profile);

            CommonRequest request = new CommonRequest();
            //request.setProtocol(ProtocolType.HTTPS);
            //设置参数
            request.setMethod(MethodType.POST);
            request.setDomain("dysmsapi.aliyuncs.com");
            request.setVersion("2017-05-25");
            request.setAction("SendSms");

            request.putQueryParameter("PhoneNumbers", phone);
            request.putQueryParameter("SignName", "我的学知网在线教育网站");//签名名称
            request.putQueryParameter("TemplateCode", "SMS_474085422");//模板code
            request.putQueryParameter("TemplateParam", JSONObject.toJSONString(param));//验证码

            try {
                CommonResponse response = client.getCommonResponse(request);
                System.out.println(response.getData());
                return response.getHttpResponse().isSuccess();
            } catch (ClientException e) {
                e.printStackTrace();
            }
            return false;
        }
}
